import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.print.*;
import java.io.*;
import javax.swing.tree.*;
import javax.swing.border.*;


public class Res_Analy extends JInternalFrame implements InterInterface,InternalFrameListener
{
	 public String roll[]=null;
	public String filesarr[] = {"","","","","",""};
	public JLabel lab24=new JLabel("Roll No:");
	public JLabel lab25=new JLabel("Ans File:");
	public JComboBox bo1 ;
	public JComboBox bo2 = new JComboBox(filesarr);
	public static int s111  = 0;
	public static int s222  = 0;
	//public String ra = new String();
	
	public Res_Analy()
	{
		super("Analysis The Examination",false,true,false,false);
		setVisible(true);
		
                
                
		
	}
	public void Res_Analy1()
	{
		
		File dir = new File("Ansfile");
		roll = dir.list();
		bo1 = new JComboBox(roll);
		
		//bo2 = new JComboBox(filesarr);
		
		bo1.addItemListener( new ItemListener() {
			public void itemStateChanged(ItemEvent ev)
			{
				if(ev.getStateChange() == ItemEvent.SELECTED)
				{
				System.out.println(bo1.getSelectedItem());
				//ra = bo1.getSelectedItem();
				num.setText("");
				total.setText("");
				bo2.removeAllItems();
				File files = new File("Ansfile/"+bo1.getSelectedItem());
				filesarr = files.list();
				for(int i = 0; i < filesarr.length; i++) {
					bo2.addItem(filesarr[i]);
				}
				
				
				}
			}
		}
		);
		bo2.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange() == ItemEvent.SELECTED) {
					System.out.println(bo2.getSelectedItem());
			File afile = new File("Ansfile/"+bo1.getSelectedItem()+"/"+bo2.getSelectedItem());
			//exlab.setText(""+bo2.getSelectedItem());
			jtabbedpane1.setTitleAt(0,""+bo2.getSelectedItem());
			try
			{
				int size = (int)afile.length();
				int char_reads = 0;
				FileReader in = new FileReader(afile);
				char data[] = new char[size];
				while(in.ready())
				{
					char_reads += in.read(data,char_reads,size-char_reads);
				}
				in.close();
				txa.setText(new String(data,0,char_reads));
			}
			catch(IOException ioe)
			{
				JOptionPane.showMessageDialog(null,"Error Openning the file","Error",JOptionPane.ERROR_MESSAGE);
			}
				}
			}
		}
		);
		
		
		Container c = getContentPane();
		c.setLayout(null);
		this.setBounds(30,10,500,400);
		lab24.setBounds(10,10,100,23);
		lab25.setBounds(130,10,100,23);
		bo1.setBounds(10,34,100,23);
		bo2.setBounds(130,34,100,23);
		lab21.setBounds(250,10,80,23);
		lab22.setBounds(250,40,80,23);
		num.setBounds(310,10,80,23);
		total.setBounds(310,40,80,23);
		bt23.setBounds(400,4,85,35);
		bt24.setBounds(400,28,85,35);
		viewbutton.setBounds(400,52,85,35);
		jtabbedpane1.setBounds(2,70,485,300);
		c.add(bo1);
		c.add(bo2);
		c.add(lab21);
		c.add(lab22);
		c.add(lab24);
		c.add(lab25);
		c.add(num);
		c.add(total);
		c.add(bt23);
		c.add(bt24);
		c.add(viewbutton);
		 c.add(jtabbedpane1);
		
		jtabbedpane1.addTab("File.....", new ImageIcon("images/middle.gif"), s, "file");
		
		
                
                txa.setBackground(Color.white);
               
		try
		{
			UIManager.setLookAndFeel(looks[1].getClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		bt23.addActionListener(new ActionListener()
		{
		public void actionPerformed(ActionEvent e)
		{
			numAdd();
		}
		}
		);
		bt24.addActionListener(new ActionListener()
		{
		public void actionPerformed(ActionEvent e)
		{
			numSave();
		}
		}
		);
		viewbutton.addActionListener(new ActionListener()
		{
		public void actionPerformed(ActionEvent e)
		{
			numTotal();
		}
		}
		);
		
                }
                
    public void internalFrameClosing(InternalFrameEvent e) {
    	//this.setVisible(false);
    	s111=0;
   }

    public void internalFrameClosed(InternalFrameEvent e) {
    	//this.setVisible(false);
    	s111 = 0;
    }

    public void internalFrameOpened(InternalFrameEvent e) {
    	//this.setVisible(true);
    }

    public void internalFrameIconified(InternalFrameEvent e) {
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
    
    }

    public void internalFrameActivated(InternalFrameEvent e) {
     }
  public void internalFrameDeactivated(InternalFrameEvent e) {
   }	
		
	private void numAdd() {
		int ra1 = 0;
		int ra2 = 0;
		String ranum = num.getText();
		String ratotal = total.getText();
		
		if(ranum.equals("")){
		JOptionPane.showMessageDialog(this,"Please Number In","Information",JOptionPane.INFORMATION_MESSAGE);
		return;}
		else 
		try {
		ra1 = Integer.parseInt(ranum);
		}
		catch(Exception e)	{
			JOptionPane.showMessageDialog(this,"Please Enter a Valid Number","Information",JOptionPane.INFORMATION_MESSAGE);
			num.setText("");
		}
		try {
		ra2 = Integer.parseInt(ratotal);
		}
		catch(Exception ex){
			ra2 = 0;
		}
					
		if(ratotal.equals(""))
		ra2 = 0;
		ra2 = ra2 + ra1;
		total.setText(Integer.toString(ra2));
		
	}
	private void numSave() {
		roll_no.setText(""+bo1.getSelectedItem()); 
		marks.setText(total.getText()); 
		SimpleTable te = new SimpleTable();
		te.SimpleTableSave();
		
		}
		
		
		
		
	private void numTotal() {
		
		
		
		
		if(s111 == 0) {
		SimpleTable te = new SimpleTable();
		te.SimpleTableView();
		desktop.add(te);
		s111 = 1;
				try{
				te.addInternalFrameListener(this);
				te.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
		
	}
	}
	
}
